import platform = require('../../../../../platform/common/baseCommon');
import bpCommon = require('../../../../../platform/common/bpCommon');
import appClient = require('../../../../../platform/client/appClient');


let moduleName = "platform.client";
let loginFactory = new appClient.asyncFactory(bpCommon.IUserLogin, "remoteCall", "loginService");
let login = <bpCommon.IUserLogin>loginFactory.getValue();

@platform.addon("登录视图", "登录视图", moduleName)
@appClient.template('views/userinfo/login.html')
export class loginView extends appClient.baseControl {
    /**
     * 获取验证码按钮
     */
    $btnGetCode?: JQuery = undefined;
    /**
     * 短信模板id
     */
    tpl_id?: number;
    /**
     * 验证码长度
     */
    codeLen?: number;
    /**
     * 手机登录按钮
     */
    $btnLoginMobile?: JQuery = undefined;
    /**
     * 输入手机号文本
     */
    $txtMobile?: JQuery = undefined;
    /**
     * 输入验证码文本
     */
    $txtVerCode?: JQuery = undefined;
    /**
     * 输入用户名文本
     */
    $txtAccount?: JQuery = undefined;
    /**
     * 输入密码文本
     */
    $txtPassword?: JQuery = undefined;

    /**
     * 帐号登录按钮
     */
    $btnLoginAccount?: JQuery = undefined;

    /**
     * 验证码时间间隔
     */
    maxCount?: number = 30;

    private curCount?: number;

    InterValObj?: number;

    onAfterBind?() {
        let that = this;
        /**
        * 帐号登录按钮事件
        */
        this.$btnLoginAccount.click(() => {
            let userinfo: platform.userInfo = new platform.userInfo();
            let usertoken: platform.userToken = new platform.userToken();
            userinfo.loginType = "Account";
            if (this.$txtAccount.val().length < 1) {
                alert("登录帐号不能为空！");
                return;
            }
            if (this.$txtPassword.val().length < 1) {
                alert("登录密码不能为空！");
                return;
            }
            userinfo.logingAccount = this.$txtAccount.val();
            userinfo.password = this.$txtPassword.val();
            usertoken.userinfo = userinfo;
            platform.currentUser = usertoken;
            try {
                login.login(usertoken).then(rs => {
                    let token = rs.body;
                    if (rs.header.rtnCode == "00" && token.userinfo.status == "00") {
                        platform.currentUser = token;
                        appClient.mainApplication.mainForm.navigate('./project/test/config/client/interactivePlatformOfBankTax/userinfo/personalpageViewConfig', '我的');
                    }
                    if (rs.header.rtnCode == "01") {
                        alert(rs.header.rtnMsg.message);
                    }
                });
            } catch (error) {
                //console.log(error.message.responseText);
                alert(JSON.parse(error.message).responseText);
            }
            
        });
        /**
       * 获取验证码按钮事件
       */
        this.$btnGetCode.click(() => {
            let mobile: string = this.$txtMobile.val();
            if (mobile.length < 11) {
                alert("请输入11位手机号码！");
                return;
            }
            
            let userinfo: platform.userInfo = new platform.userInfo();
            let usertoken: platform.userToken = new platform.userToken();
            userinfo.loginType = "Mobile";
            userinfo.mobile = this.$txtMobile.val();
            usertoken.userinfo = userinfo;
            platform.currentUser = usertoken;
            try {
                login.login(usertoken).then(rs => {
                    let token = rs.body;
                    if (rs.header.rtnCode == "00" && token.userinfo.status == "00") {
                        platform.currentUser = token;
                        try {
                            that.curCount = that.maxCount;
                            that.InterValObj = window.setInterval(() => {
                                that.curCount--;
                                if (that.curCount == 0) {
                                    window.clearInterval(that.InterValObj);//停止计时器
                                    that.$btnGetCode.text("重新获取");
                                    that.$btnGetCode.removeClass("disabled");
                                }
                                else {
                                    that.$btnGetCode.text(that.curCount + "秒后重发");
                                }
                            }, 1000);
                            that.$btnGetCode.text(that.maxCount + "秒后重发");
                            that.$btnGetCode.addClass("disabled");
                            that.$btnLoginMobile.removeClass("disabled");
                            debugger;
                            login.getCode(mobile, that.tpl_id, that.codeLen);
                        } catch (error) {
                            console.log(error.message.responseText);
                            alert(JSON.parse(error.message).responseText);
                        }
                    }
                    if (rs.header.rtnCode == "01") {
                        alert(rs.header.rtnMsg.message);
                    }
                });
            } catch (error) {
                //console.log(error.message.responseText);
                alert(JSON.parse(error.message).responseText);
            }


        });

        /**
         * 手机登录按钮事件
         */
        this.$btnLoginMobile.click(() => {
            let mobile: string = this.$txtMobile.val();
            let tempCode: string = this.$txtVerCode.val();
            login.codelogin(mobile,tempCode).then((rs) => {
                if (rs.header.rtnCode == "01") {
                    alert(rs.header.rtnMsg.message);
                    return;
                }
                if(platform.currentUser.userinfo.status=="00"){
                    appClient.mainApplication.mainForm.navigate('./project/test/config/client/interactivePlatformOfBankTax/userinfo/personalpageViewConfig', '个人首页');
                }
            })
        });
    }
}